﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Charm : Buff, IAIBuff
    {
        private string targetsFaction = "";
        private CreatureBase castingCreature;

        public Spell_Charm()
        {
            minutesLeft = 1;
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            castingCreature = myCreatureBase;

            if (myCreatureBase is Character)
            {
                Character character = (Character)myCreatureBase;

                if (TargetCreatureBase is Trainer || TargetCreatureBase is Vendor || TargetCreatureBase is Banker || TargetCreatureBase is QuestCreature || TargetCreatureBase.CreatureType == "Binder" || TargetCreatureBase.CreatureType == "AlchemyTrainer")
                {
                    Message msg = new Message();
                    msg.Type = "server";
                    msg.CreatureID = myCreatureBase.CreatureID;
                    msg.Text = "You cannot charm that creature";

                    character.AddMessage(msg);
                    return;
                }
                if (TargetCreatureBase == myCreatureBase)
                {
                    Message msg = new Message();
                    msg.Type = "server";
                    msg.CreatureID = myCreatureBase.CreatureID;
                    msg.Text = "You cannot charm yourself";

                    character.AddMessage(msg);
                    return;
                }
                if (TargetCreatureBase.petController != null)
                {
                    Message msg = new Message();
                    msg.Type = "server";
                    msg.CreatureID = myCreatureBase.CreatureID;
                    msg.Text = "You cannot charm an existing pet";

                    character.AddMessage(msg);
                    return;
                }
                if (character.petManager.NumPets() > 3)
                {
                    Message msg = new Message();
                    msg.Type = "server";
                    msg.CreatureID = myCreatureBase.CreatureID;
                    msg.Text = "You have to many pets";

                    character.AddMessage(msg);
                    return;
                }
                foreach (Pet_Controller pet in character.petManager.GetPets())
                {
                    if (pet.CharmBuff != null)
                    {
                        Message msg = new Message();
                        msg.Type = "server";
                        msg.CreatureID = myCreatureBase.CreatureID;
                        msg.Text = "You cannot charm more than 1 creature";

                        character.AddMessage(msg);
                        return;
                    }
                }

                targetsFaction = TargetCreatureBase.Faction;
                TargetCreatureBase.Faction = myCreatureBase.Faction;

                MapItem currentMapItem = null;
                foreach (MapItem mapItem in MapItemsWithin12)
                {
                    if (mapItem.MapX == myCreatureBase.MapX && mapItem.MapY == myCreatureBase.MapY)
                    {
                        currentMapItem = mapItem;
                        break;
                    }
                }

                TargetCreatureBase.petController = new Pet_Controller(TargetCreatureBase, myCreatureBase, currentMapItem);
                TargetCreatureBase.petController.CharmBuff = this;
                TargetCreatureBase.buffManager.UpdateBuffs();

                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;
                    targetCharacter.SetBusy(true);

                    //If character is waiting for round to expire, spell needs to wait
                    if (targetCharacter.Timer_Round_IsRunning == true)
                    {
                        //Handler to fire after round timer has elapsed
                        targetCharacter.RoundElapsed += new Character.TimerElapsedHandler(targetCharacter_RoundElapsed);
                    }
                    //Character is not waiting for round, start spell immediately
                    else
                    {
                        //Start Spell
                        targetCharacter.StartAI();
                    }
                }

                //Start Spell Timer
                spellTimer.Start();

                character.petManager.AddPet(TargetCreatureBase.petController);

                foreach (MapItem mapitem in MapItemsWithin12)
                {
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character ctr = (Character)creaturebase;
                                ctr.AddCreatureBase(TargetCreatureBase);
                            }
                        }
                    }
                }
            }
        }

        private void targetCharacter_RoundElapsed(object sender)
        {
            Character character = (Character)base.TargetCreatureBase;
            //Remove handler, we dont want to restart the spell after the next round
            character.RoundElapsed -= new Character.TimerElapsedHandler(targetCharacter_RoundElapsed);

            //Start Spell
            character.StartAI();
        }

        public int runAI()
        {
            if (TargetCreatureBase.petController != null)
                return TargetCreatureBase.petController.RunPetAI();

            return 3000;
        }

        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                base.RemoveSpell(updateBuffs, sendMessage);

                if (castingCreature is Character)
                {
                    Character character = (Character)castingCreature;
                    character.petManager.RemovePet(TargetCreatureBase.petController);
                }
                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;
                    targetCharacter.SetBusy(false);
                }

                TargetCreatureBase.Faction = targetsFaction;
                TargetCreatureBase.petController = null;

                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character ctr = (Character)creaturebase;
                                ctr.AddCreatureBase(TargetCreatureBase);
                            }
                        }
                    }
                }
            }
        }
    }
}
